﻿using System.Web;
using System.Web.UI;
using Trentacular.SharePoint.Util;
using Trentacular.Office.Server.Search.FacetedSearch;

namespace Trentacular.Office.Server.Web.UI.WebControls
{
    /// <summary>
    /// Default Facet Renderer that generates a hyperlink and a count.
    /// </summary>
    public class DefaultFacetRenderer : Control
    {
        /// <summary>
        /// Gets or sets the facet.
        /// </summary>
        /// <value>The facet.</value>
        public Facet Facet { get; set; }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use
        /// composition-based implementation to create any child controls they contain
        /// in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Add(
                new LiteralControl(
                    string.Format(
                        @"<a href=""#"" title=""{0}"" onclick=""return false;"">{1}</a> ({2})",
                        HttpUtility.HtmlAttributeEncode(Facet.DisplayName),
                        HttpUtility.HtmlEncode(
                            Facet.DisplayName.TruncateText(
                                Facet.Configuration.MaxCharactersToDisplay,
                                false,
                                false
                                )
                            ),
                        Facet.Count
                        )
                    )
                );

            base.CreateChildControls();
        }
    }
}
